package com.spic.business.accident.instant.controller;

import com.spic.business.accident.instant.domain.AccidentInstantReport;
import com.spic.business.accident.instant.service.IAccidentInstantReportService;
import com.spic.business.common.core.workflow.IWorkFlowService;
import com.spic.business.common.core.workflow.WorkFlowController;
import com.spic.common.core.utils.poi.ExcelUtil;
import com.spic.common.core.web.domain.AjaxResult;
import com.spic.common.core.web.page.TableDataInfo;
import com.spic.common.log.annotation.Log;
import com.spic.common.log.enums.BusinessType;
import com.spic.common.security.annotation.PreAuthorize;
import com.spic.system.api.RemoteDeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 事故即时报告Controller
 *
 * @author spic
 * @date 2021-11-30
 */
@RestController
@RequestMapping("accident/instant")
public class AccidentInstantReportController extends WorkFlowController {
    @Autowired
    private IAccidentInstantReportService accidentInstantReportService;
    @Autowired
    private RemoteDeptService remoteDeptService;

    /**
     * 查询事故即时报告列表
     */
    @PreAuthorize(hasPermi = "business/accident:instant:list")
    @GetMapping("/list")
    public TableDataInfo list(AccidentInstantReport accidentInstantReport) {
        //获取部门及子部门
        if (null != accidentInstantReport.getDeptId()) {
            List<Long> deptIds = remoteDeptService.getDeptAndChildren(accidentInstantReport.getDeptId()).getData();
            accidentInstantReport.setDeptIds(deptIds);
        }
        startPage();
        List<AccidentInstantReport> list = accidentInstantReportService.selectAccidentInstantReportList(accidentInstantReport);
        return getDataTable(list);
    }

    /**
     * 导出事故即时报告列表
     */
    @PreAuthorize(hasPermi = "business/accident:instant:export")
    @Log(title = "事故即时报告", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, AccidentInstantReport accidentInstantReport) throws IOException {
        List<AccidentInstantReport> list = accidentInstantReportService.selectAccidentInstantReportList(accidentInstantReport);
        ExcelUtil<AccidentInstantReport> util = new ExcelUtil<AccidentInstantReport>(AccidentInstantReport.class);
        util.exportExcel(response, list, "事故即时报告数据");
    }

    /**
     * 获取事故即时报告详细信息
     */
    @PreAuthorize(hasPermi = "business/accident:instant:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(accidentInstantReportService.selectAccidentInstantReportById(id));
    }

    /**
     * 新增事故即时报告
     */
    @PreAuthorize(hasPermi = "business/accident:instant:add")
    @Log(title = "事故即时报告", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody AccidentInstantReport accidentInstantReport) {
        return AjaxResult.success(accidentInstantReportService.insertAccidentInstantReport(accidentInstantReport));
    }

    /**
     * 修改事故即时报告
     */
    @PreAuthorize(hasPermi = "business/accident:instant:edit")
    @Log(title = "事故即时报告", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody AccidentInstantReport accidentInstantReport) {
        return toAjax(accidentInstantReportService.updateAccidentInstantReport(accidentInstantReport));
    }

    /**
     * 删除事故即时报告
     */
    @PreAuthorize(hasPermi = "business/accident:instant:remove")
    @Log(title = "事故即时报告", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(accidentInstantReportService.deleteAccidentInstantReportByIds(ids));
    }

    @Override
    protected IWorkFlowService getService() {
        return accidentInstantReportService;
    }
}
